package es.unex.meigas.gvsig.gui;

import java.util.ArrayList;

import com.iver.andami.PluginServices;
import com.iver.andami.ui.mdiManager.IWindow;
import com.iver.cit.gvsig.ProjectExtension;
import com.iver.cit.gvsig.fmap.layers.FLayer;
import com.iver.cit.gvsig.fmap.layers.FLayers;
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
import com.iver.cit.gvsig.fmap.layers.LayersIterator;
import com.iver.cit.gvsig.project.Project;
import com.iver.cit.gvsig.project.ProjectFactory;
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
import com.iver.cit.gvsig.project.documents.view.ProjectView;
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;

import es.unex.meigas.dataObjects.AbstractInputFactory;
import es.unex.meigas.dataObjects.IDataObject;
import es.unex.meigas.dataObjects.ILayer;
import es.unex.meigas.gvsig.core.FileTools;
import es.unex.meigas.gvsig.core.gvRasterLayer;
import es.unex.meigas.gvsig.core.gvTable;
import es.unex.meigas.gvsig.core.gvVectorLayer;

public class gvInputFactory extends AbstractInputFactory {

	public void createDataObjects() {

		ArrayList list = new ArrayList();
		Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
		ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);

		for (int i = 0; i < views.size(); i++) {
			ProjectView view = (ProjectView) views.get(i);
			FLayers layers = view.getMapContext().getLayers();
			LayersIterator iter = new LayersIterator(layers);

			while (iter.hasNext()){
				FLayer layer = iter.nextLayer();
				if (layer instanceof FLyrRaster){
					gvRasterLayer rasterLayer =new gvRasterLayer();
					rasterLayer.create((FLyrRaster)layer);
					list.add(rasterLayer);
				}
				else if (layer instanceof FLyrVect){
					gvVectorLayer vectorLayer = new gvVectorLayer();
					vectorLayer.create((FLyrVect)layer);
					list.add(vectorLayer);
				}
			}
		}

		ArrayList tables = project.getDocumentsByType(ProjectTableFactory.registerName);
		for (int i = 0; i < tables.size(); i++) {
			gvTable table = new gvTable();
			table.create((ProjectTable) tables.get(i));
			list.add(table);
		}

		m_Objects =  new IDataObject[list.size()];
		for (int i = 0; i < list.size(); i++) {
			m_Objects[i] = (IDataObject)list.get(i);
		}

	}


	public String[] getRasterLayerInputExtensions() {

		return FileTools.RASTER_EXT_IN;

	}

	public String[] getVectorLayerInputExtensions() {

		return FileTools.VECTOR_EXT_IN;

	}

	public String[] getTableInputExtensions() {

		return FileTools.TABLE_EXT;

	}

	public IDataObject openDataObjectFromFile(String sFilename) {

		Object object = FileTools.open(sFilename);

		if (object == null){
			return null;
		}
		else if (object instanceof FLyrRaster){
			gvRasterLayer layer = new gvRasterLayer();
			layer.create(object);
			return layer;
		}
		else if (object instanceof FLyrVect){
			gvVectorLayer layer = new gvVectorLayer();
			layer.create((FLyrVect)object);
			return layer;
		}
		else if (object instanceof ProjectTable){
			gvTable table = new gvTable();
			table.create((ProjectTable)object);
			return table;
		}
		else{
			return null;
		}

	}

	public void addDataObjectToGIS(IDataObject obj){

		ProjectView view = getView();

		if (obj instanceof ILayer){
			FLayer layer = (FLayer) ((ILayer)obj).getBaseDataObject();
			view.getMapContext().getLayers().addLayer(layer);
		}

	}


	private ProjectView getView() {

		Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
		ArrayList views = project.getDocumentsByType(ProjectViewFactory.registerName);

		for (int i = 0; i < views.size(); i++) {
			ProjectView view = (ProjectView) views.get(i);
			if (view.getName().equals("MEIGAS")){
				return view;
			}
		}

		final ProjectView newView = ProjectFactory.createView("MEIGAS");
        ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class))
        										.getProject().addDocument(newView);
        IWindow window = newView.createWindow();
        PluginServices.getMDIManager().addWindow(window);

		return newView;

	}

}
